package work.xkong.controller;

import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.xkong.common.ResponseRest;
import work.xkong.entity.Student;
import work.xkong.service.IStudentService;
import work.xkong.service.ISystemService;
import work.xkong.utils.TokenUtils;
import work.xkong.vo.inputvo.LoginInfoInputVo;
import work.xkong.vo.outputvo.LastLoginTimeOutputVo;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @PACKAGE_NAME: work.xkong.controller
 * @NAME: SystemController
 * @USER: NuanYang flyang
 * @author: 查炼飞
 * @create: 2022/11/21 17:08
 */
@RestController
@RequestMapping("/systems")
public class SystemController {
    @Autowired
    private ISystemService systemService;
    @Autowired
    private IStudentService studentService;

    @PostMapping("/login")
    public ResponseRest login(LoginInfoInputVo loginInfoInput) {
        System.out.println(loginInfoInput.toString());
        return systemService.doLogin(loginInfoInput);
    }

    @GetMapping("/getTime")
    public ResponseRest getTime(HttpServletRequest request) {
        String jwt = request.getHeader("Authorization");

        Claims claims = TokenUtils.getParseToken(jwt);
        String id = (String) claims.get(Claims.ID);

        Student student = studentService.getById(Integer.parseInt(id));

        LastLoginTimeOutputVo lastLoginTimeOutputVo = new LastLoginTimeOutputVo();

        lastLoginTimeOutputVo.setLastLoginTime(student == null ? LocalDateTime.now() : student.getStudentLastLoginTime());

        return ResponseRest.creatSuccess(lastLoginTimeOutputVo);
    }

}
